{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Kudryavtsev Model\n",
    "\n",
    "\n",
    "### Introduction to Permafrost Processes - Lesson 2 Kudryavtsev Model\n",
    "\n",
    "This lab has been designed and developed by Irina Overeem and Mark Piper, CSDMS, University of Colorado, CO \n",
    "with assistance of Kang Wang, Scott Stewart at CSDMS, University of Colorado, CO, and Elchin Jafarov, at Los Alamos National Labs, NM. \n",
    "These labs are developed with support from NSF Grant 1503559, ‘Towards a Tiered Permafrost Modeling Cyberinfrastructure’ \n",
    "\n",
    "### Classroom organization\n",
    "\n",
    "This lab is the second in a series of introduction to permafrost process modeling, designed for inexperienced users. In this first lesson, we explore the Air Frost Number model and learn to use the CSDMS Python Modeling Toolkit ([PyMT](https://github.com/csdms/pymt)). We implemented a basic configuration of the Air Frost Number (as formulated by Nelson and Outcalt in 1987). This series of labs is designed for inexperienced modelers to gain some experience with running a numerical model, changing model inputs, and analyzing model output. Specifically, this first lab looks at what controls permafrost occurrence and compares the occurrence of permafrost in Russia. \n",
    "Basic theory on the Air Frost Number is presented in [Frost Number Model Lecture 1](https://csdms.colorado.edu/wiki/File:FrostNumberModel_Lecture1.pptx).\n",
    "\n",
    "This lab is the second in a series of introduction to permafrost process modeling, designed for inexperienced users. In this second lesson, we explore the Kudryavstev model and learn to use the CSDMS Python Modeling Toolkit ([PyMT](https://github.com/csdms/pymt)). We implemented the Kudryavstev model (as formulated in Anisimov et al.1997). It is dubbed the Ku-model. This series of labs is designed for inexperienced modelers to gain some experience with running a numerical model, changing model inputs, and analyzing model output. Specifically, this lab looks at what controls soil temperature and active layer thickness and compares model output with observed longterm data collected at permafrost active layer thickness monitoring sites in Fairbanks and Barrow, Alaska. \n",
    "Basic theory on the Kudryavstev model is presented in [Kudryavtsev Model Lecture 2](https://csdms.colorado.edu/wiki/File:KudryavtsevModel_Lecture2.pptx)\n",
    "\n",
    "This lab will likely take ~ 1,5 hours to complete in the classroom. This time assumes you are unfamiiar with the PyMT and need to learn setting parameters, saving runs, downloading data and looking at output (otherwise it will be much faster).\n",
    "\n",
    "We will use netcdf files for output, this is a standard output from all CSDMS models. If you have no experience with visualizing these files, Panoply software will be helpful. Find instructions on how to use this software.\n",
    "\n",
    "### Learning objectives\n",
    "\n",
    "#### Skills\n",
    "\n",
    "* familiarize with a basic configuration of the Kudryavstev Model for 1D (a single location).\n",
    "* hands-on experience with visualizing NetCDF time series with Panoply.\n",
    "* data to model comparisons and how to think about uncertainty in data and model output.\n",
    "\n",
    "#### Topical learning objectives:\n",
    "\n",
    "* what are controls on permafrost soil temperature\n",
    "* what is a steady-state model\n",
    "* what are important parameters for calculating active layer thickness\n",
    "* active layer thickness evolution with climate warming in two locations in Alaska\n",
    "\n",
    "### References and More information \n",
    "\n",
    "Anisimov, O. A., Shiklomanov, N. I., & Nelson, F. E. (1997). *Global warming and active-layer thickness: results from transient general circulation models.* Global and Planetary Change, 15(3-4), 61-77. DOI:10.1016/S0921-8181(97)00009-X  \n",
    "Sazonova, T.S., Romanovsky, V.E., 2003. *A model for regional-scale estimation of temporal and spatial variability of active layer thickness and mean nnaual ground emperatures.* Permafrost and periglacial processes 14, 125-139. DOI: 10.1002/ppp.449  \n",
    "Zhang, T., 2005. *Influence of the seasonal snow cover on the ground thermal regime: an overview.* Review of Geophysics, 43, RG4002.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Kudryavtsev Model\n",
    "\n",
    "The Kudryavtsev et al. (1974), or *Ku* model, presents an\n",
    "approximate solution of the Stefan problem. The model provides a\n",
    "steady-state solution under the assumption of sinusoidal air\n",
    "temperature forcing. It considers snow, vegetation, and soil layers\n",
    "as thermal damping to variation of air temperature. The layer of\n",
    "soil is considered to be a homogeneous column with different thermal\n",
    "properties in the frozen and thawed states. The main outputs are\n",
    "annual maximum frozen/thaw depth and mean annual temperature at the\n",
    "top of permafrost (or at the base of the active layer). It can be\n",
    "applied over a wide variety of climatic conditions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymt.models\n",
    "ku = pymt.models.Ku()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 1\n",
    "\n",
    "We will run the Kudryatsev model for conditions in Barrow, Alaska in a very cold year, 1964. The mean annaul temperature for 1964 was -15.21C, the amplitude over that year was 18.51C. It was close to normal snow year, meaning the average snow thickness over this winter was 0.22m.\n",
    "\n",
    "Adapt the settings in the Ku model for Barrow 1964. Make sure you request an output file. Save the simulation settings and submit your simulation. Download the model results and open them in Panoply."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_file, run_folder = ku.setup(T_air=-15.21, A_air=18.51)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.initialize(config_file, run_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.output_var_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q1.1: What was the active layer thickness the model predicted? \n",
    "\n",
    "*Sketch a soil profile for winter conditions versus August conditions, indicate where the frozen-unfrozen boundary is in each two cases.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q1.2: How do you think snow affects the active layer thickness predictions? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 2\n",
    "\n",
    "Run the Kudryatsev model with a range of snow conditions (0 m as the one extreme, and in extremely snowy years, the mean snow thickness over the winter is 0.4m in Barrow). Set these two simulations, run them and dowload the files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "args = ku.setup(h_snow=0.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.initialize(*args)\n",
    "ku.update()\n",
    "ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "args = ku.setup(h_snow=0.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku.initialize(*args)\n",
    "ku.update()\n",
    "ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q2.1: What happens if there is no snow at all (0 m)?  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q2.2: What is the active layer thickness prediction for a very snowy year?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 3\n",
    "\n",
    "Run the Kudryatsev model with a range of soil water contents. What happens if there is 20% more, and 20% less soil water content? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "args = ku.setup(vwc_H2O=0.2)\n",
    "ku.initialize(*args)\n",
    "ku.update()\n",
    "ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "args = ku.setup(vwc_H2O=0.6)\n",
    "ku.initialize(*args)\n",
    "ku.update()\n",
    "ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q3.1: Is this selected range of 20% realistic for soils in permafrost regions?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Q3.2: From the theory presented in the associated lecture notes, how do you think soil water content in summer affects the soil temperature?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 4\n",
    "\n",
    "Posted here are time-series for climate conditions for both Barrow and Fairbanks, Alaska. Time-series are annual values and run from 1961-2015, the data include mean annual temperature (MAAT), temperature amplitude (TAMP) and winter-average snow depth (SD). \n",
    "\n",
    "These are text files, so you can plot them in your own favorite software or programming language.\n",
    "\n",
    "Choose which case you want to run, you will now run a 55 year simulation. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "data = pandas.read_csv(\"https://raw.githubusercontent.com/mcflugen/pymt_ku/master/data/Barrow_1961-2015.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "maat = data[\"atmosphere_bottom_air__temperature\"]\n",
    "tamp = data[\"atmosphere_bottom_air__temperature_amplitude\"]\n",
    "snow_depth = data[\"snowpack__depth\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ku = pymt.models.Ku()\n",
    "args = ku.setup(end_year=2050)\n",
    "ku.initialize(*args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_steps = int((ku.end_time - ku.time) / ku.time_step)\n",
    "thickness = np.empty(n_steps)\n",
    "for i in range(n_steps):\n",
    "    ku.set_value(\"atmosphere_bottom_air__temperature\", maat.values[i])\n",
    "    ku.set_value(\"atmosphere_bottom_air__temperature_amplitude\", tamp.values[i])\n",
    "    ku.set_value(\"snowpack__depth\", snow_depth.values[i])\n",
    "    ku.update()\n",
    "    thickness[i] = ku.get_value('soil__active_layer_thickness')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(thickness) # This should be the same as the above but it's NOT! But now it is. BOOM!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
